

#ifndef WKPLAYER_WKAUDIO_H
#define WKPLAYER_WKAUDIO_H

#include "SoundTouch.h"
using namespace soundtouch;

extern "C"
{
#include "libavcodec/avcodec.h"
#include <libswresample/swresample.h>
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
#include <libavutil/time.h>
};

#include "PlayerStatus.h"
#include "WkQueue.h"
#include "WkJavaCall.h"

class WkAudio {

public:
    int streamIndex = -1 ;
    AVCodecParameters* avCodecParameters ;
    AVCodecContext * avCodecContext ;
    WkQueue* queuePacket =NULL;
    PlayerStatus* playerStatus =NULL ;
    WkJavaCall * wkJavaCall ;

    pthread_t thread_play ;
    AVPacket *avPacket = NULL;
    AVFrame *avFrame = NULL;
    int ret = 0;
    uint8_t *buffer = NULL;
    int data_size = 0;
    int sample_rate = 0;

    //音频总时长
    int duration = 0;
    AVRational time_base;
    double clock;//总的播放时长
    double now_time;//当前frame时间
    double last_tiem; //上一次调用时间

    int volumePercent = 100;
    int mute = 2;

    float pitch = 1.0f;
    float speed = 1.0f;

    bool isRecordPcm = false;


    // 引擎接口
    SLObjectItf engineObject = NULL;
    SLEngineItf engineEngine = NULL;

    //混音器
    SLObjectItf outputMixObject = NULL;
    SLEnvironmentalReverbItf outputMixEnvironmentalReverb = NULL;
    SLEnvironmentalReverbSettings reverbSettings = SL_I3DL2_ENVIRONMENT_PRESET_STONECORRIDOR;

    //pcm
    SLObjectItf pcmPlayerObject = NULL;
    SLPlayItf pcmPlayerPlay = NULL;
    SLVolumeItf pcmVolumePlay = NULL;
    SLMuteSoloItf  pcmMutePlay = NULL;

    //缓冲器队列接口
    SLAndroidSimpleBufferQueueItf pcmBufferQueue = NULL;

    //SoundTouch
    SoundTouch *soundTouch = NULL;
    SAMPLETYPE *sampleBuffer = NULL;
    bool finished = true;
    uint8_t *out_buffer = NULL;
    int nb = 0;
    int num = 0;




public:
    WkAudio(PlayerStatus* playerStatus , int sampRate , WkJavaCall * wkJavaCall);
    ~WkAudio();
    void play();
    void initOpenSLES();
    int resampleAudio(void **pcmbuf);
    SLuint32 getCurrentSampleRateForOpensles(int sample_rate);
    void pause();
    void resume();
    void stop();
    void release();
    void setVolume(int percent);
    void setMute(int mute);
    int getSoundTouchData();
    void setPitch(float pitch);
    void setSpeed(float speed);
    int getPCMDB(char *pcmcata, size_t pcmsize);
    void startStopRecord(bool start);
//    void setCircle(bool isCircle);
};

#endif //WKPLAYER_WKAUDIO_H

